﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using ThreeByte.Media;

namespace ThreeByte.ActiveDeck.Content
{
    /// <summary>
    /// Interaction logic for QuestionPreview.xaml
    /// </summary>
    public partial class QuestionPreview : UserControl, IPreviewControl
    {
        public PageConfig Config { get; set; }

        public QuestionPreview(QuestionConfig config) {
            InitializeComponent();
            Config = config;
            DataContext = config;
            config.Refresh();
        }

        //Happens on Dispatcher Thread
        public PresentationPageConfiguration GetPageConfiguration() {
            XNamespace ns = Service.ServiceConfiguration.NAMESPACE;

            QuestionConfig config = (QuestionConfig)DataContext;
            PresentationPageConfiguration newPage = new PresentationPageConfiguration();
            XElement pageNode = config.GetPresentationXml("ShortAnswerQuestion");

            pageNode.Add(config.GetGradingConfig());

            //Disable the Interactive Elements
            LayerRenderer.Hide();
            //Snap the Background
            this.UpdateLayout();
            GraphicResource backgroundResource = new GraphicResource(ImageUtil.SaveToBitmapSource(LayoutRoot, ImageUtil.DefaultPortraitSize), null, ImageType.JPEG);
            newPage.Resources[backgroundResource.ID] = backgroundResource;
            XElement backgroundNode = new XElement(ns + "BackgroundImage", new XAttribute("ResourceID", backgroundResource.ID));
            pageNode.Add(backgroundNode);

            //Enable the Interactive Elements
            LayerRenderer.Show();
            this.UpdateLayout();

            //Locate the TextBox
            XElement textBoxElement = new XElement(ns + "TextBox", new XAttribute("Name", AnswerTextBox.Name));
            //FontSize
            textBoxElement.Add(new XAttribute("FontSize", (int)(AnswerTextBox.FontSize)));
            //TODO: Bind the FontColor better
            textBoxElement.Add(new XAttribute("FontColor", @"#FF000000"));
            Point textPos = AnswerTextBox.TransformToAncestor(LayoutRoot).Transform(new Point(0, 0));
            textBoxElement.Add(new XAttribute("Top", (int)(textPos.Y)));
            textBoxElement.Add(new XAttribute("Left", (int)(textPos.X)));
            textBoxElement.Add(new XAttribute("Width", (int)(AnswerTextBox.ActualWidth)));
            textBoxElement.Add(new XAttribute("Height", (int)(AnswerTextBox.ActualHeight)));
            pageNode.Add(textBoxElement);

            //Locate the Button
            XElement buttonElement = new XElement(ns + "Button");
            buttonElement.Add(ActionButton.Action.ToXml());

            Point buttonPos = ActionButton.TransformToAncestor(LayoutRoot).Transform(new Point(0, 0));
            buttonElement.Add(new XAttribute("Top", (int)(buttonPos.Y)));
            buttonElement.Add(new XAttribute("Left", (int)(buttonPos.X)));
            buttonElement.Add(new XAttribute("Width", (int)(ActionButton.ActualWidth)));
            buttonElement.Add(new XAttribute("Height", (int)(ActionButton.ActualHeight)));

            //Snap the Button
            LayerRenderer.SetActive();
            GraphicResource buttonOnResource = new GraphicResource(ImageUtil.SaveToBitmapSource(ActionButton));
            newPage.Resources[buttonOnResource.ID] = buttonOnResource;
            XElement imageOnNode = new XElement(ns + "ImageOn", new XAttribute("ResourceID", buttonOnResource.ID));
            buttonElement.Add(imageOnNode);
            this.UpdateLayout();

            //Snap the Off Button
            LayerRenderer.SetInactive();
            Brush currentBrush = ActionButton.Foreground;
            //ActionButton.Foreground = Brushes.Red;
            GraphicResource buttonOffResource = new GraphicResource(ImageUtil.SaveToBitmapSource(ActionButton));
            newPage.Resources[buttonOffResource.ID] = buttonOffResource;
            XElement imageOffNode = new XElement(ns + "ImageOff", new XAttribute("ResourceID", buttonOffResource.ID));
            buttonElement.Add(imageOffNode);
            ActionButton.Foreground = currentBrush;
            this.UpdateLayout();

            pageNode.Add(buttonElement);

            newPage.ConfigXml = pageNode;
            return newPage;
        }
    }
}
